(define selfmenuroot "<Image>/contributed")     ; Change these to suit yourself         
(define selfmenuentry "SamC...")

(define selfauthor "SamC")
(define selfcopyright "Copyright (C) 2013 SamC\nhttp://www.gimptalk.com/index.php?/topic/58951-non-homogeneous-stretching/")

; For SamC  http://www.gimptalk.com/index.php?/topic/58951-non-homogeneous-stretching/
; Version 1.0 3.10.2013 First version
; Version 1.1 8.10.2013 Make scale-factor more configurable

(define selffileversion "08.10.2013 Version 1.1 - Second Version")

(define selffilename "\n- samc.scm")

(define selftipstrip (string-append "Non homogeneous stretching" "\n\n\nRegisters in menu " selfmenuroot  "/" selfmenuentry "\n\nScript File name - " selffilename))

(define (script-fu-samc-rowcoords image-width image-height row sample-height scale-factor)
  (let* (         
;          (scale-factor 3)
          (new-ratio (- 1 (/ 1 scale-factor)))
;          (row-width (* (+ (* (/ -2 3) (/ row image-height) ) 1)  image-width ))   ; how long the re-sized line will be
          (row-width (* (- 1 (* new-ratio (/ row image-height))) image-width) )
          (row-x0 0)
          (row-y0 (/ sample-height -2))
          (row-x1 row-width)
          (row-y1 (/ sample-height 2))
          (row-x-half-length (/ (- image-width row-width) 2))
        )
;        (gimp-message (string-append (number->string row-x0) " " (number->string row-x1)))       
        (list row-x0 row-y0 row-x1 row-y1 row-x-half-length)       
  )
)

(define (script-fu-samc image drawable scale-factor)
  (let* ( 
          (image-width (car (gimp-image-width image)))
          (image-height (car (gimp-image-height image)))
          (row 0)
          (new-image -1) (new-drawable -1) (float-select -1) (float-layer -1)
          (row-coords '())       
          (sample-height 3) (y0 0)
          
          (stop-row image-height) (start-row 0)
;          (stop-row 120) (start-row 104)          ; only for debugging
        )
        (set! new-image (car (gimp-image-new image-width image-height RGB)))  ; create a new image to put things in
        (set! new-drawable (car (gimp-layer-new new-image image-width image-height RGBA-IMAGE "background" 100 NORMAL-MODE))) ; a new layer for the new image
        (gimp-image-add-layer new-image new-drawable 0) ; add the new layer to the new image

        (set! row start-row)
        (while (< row stop-row) ; scan down the source image
          (set! y0 (- row (/ sample-height 2))) ; sample the adjacent lines so the interpolation has something to work with
          (gimp-rect-select image 0 y0 image-width sample-height CHANNEL-OP-REPLACE FALSE 0.0) ; select the area on the source image
          (gimp-edit-copy drawable) ; copy the area on the source image
          (set! float-select (car (gimp-edit-paste new-drawable FALSE ))) ; paste the copied are into the new image as a floating selection

          (gimp-floating-sel-to-layer float-select) ; make the floating selection into a layer
          (set! float-layer (car (gimp-image-get-active-layer new-image))) ; get the id of the new layer

          (set! row-coords (script-fu-samc-rowcoords image-width image-height row sample-height scale-factor)) ; calculate the required corners of the re-sized layer
          (set! float-layer (car (gimp-drawable-transform-scale float-layer (car row-coords) (cadr row-coords) (caddr row-coords) (cadddr row-coords) TRANSFORM-FORWARD INTERPOLATION-CUBIC FALSE 3 TRANSFORM-RESIZE-ADJUST))) ; resize the layer

 ;         (gimp-layer-resize float-layer (caddr row-coords) 1 0 -1)

;(gimp-message (string-append (number->string (car row-coords)) " " (number->string (cadr row-coords)) " " (number->string (caddr row-coords)) " " (number->string (cadddr row-coords)) ))
          
          (gimp-layer-set-offsets float-layer (list-ref row-coords 4) row) ; move the re-sized layer to the right place
          (set! new-drawable (car (gimp-image-merge-down new-image float-layer EXPAND-AS-NECESSARY))) ; merge the re-sized layer into the background
          (set! row (+ 1 row)) ; increment the row counter
        )
        
        (gimp-display-new new-image) ; display the new image
        (gimp-displays-flush) ; update the display so the new image can be seen
        (gimp-image-clean-all image) ; Tell GIMP that making a selection doesn't alter the source image
  ) 
)

(script-fu-register "script-fu-samc"
                    selfmenuentry
                    selftipstrip
                    selfcopyright
                    selfauthor
                    selffileversion
                    ""
                    SF-IMAGE "Input Image" 0
                    SF-DRAWABLE "Input Drawable" 0
                    SF-ADJUSTMENT "Scale-Factor" '(3 1 10 1 0 1) 
                    )

(script-fu-menu-register "script-fu-samc" selfmenuroot)

